#include "tasksInit.h"

#include <stdio.h>

// #include "ohos_init.h"
#include "los_task.h"
#include "los_queue.h"
#include "los_event.h"

#include "task_autoMode.h"
#include "task_bspInit.h"
#include "task_doorCntl.h"
#include "task_keyScan.h"
#include "task_mqttHandle.h"
#include "task_sensorData.h"
#include "task_pageManage.h"
#include "task_cntlCenter.h"

#include "bsp_key.h"
#include "bsp_e53.h"

//定义相关资源

//队列
unsigned int pageManage_qId, sensorData_qId, doorCntl_qId, cntlCenter_qId, autoMode_qId;

//信号量
//事件
EVENT_CB_S bspInit_ECB;

// EVENT_CB_S autoMode_awayHome_ECB, autoMode_backHome_ECB, autoMode_manual_ECB;

/***************************************************************/

unsigned int tasksInit_id;
TSK_INIT_PARAM_S tasksInit_handler = {
    (TSK_ENTRY_FUNC)tasksInit,
    20,             //TaskPrio
    0,              //Arg
    1280,            //stack size
    "tasksInit",//Name
    0               //resved
};

void tasksInit(void)
{
    unsigned int ret = LOS_OK;

    printf("[tasksInit] Enter tasksInit.\n");

    LOS_TaskLock();

    //1. 创建资源 队列等

    //1.1 事件

    //硬件初始化事件
    ret = LOS_EventInit(&bspInit_ECB);
    if(ret != LOS_OK)
    {
        printf("[tasksInit] LOS_EventInit bspInit_ECB error, code: 0x%x\n", ret);
    }

    //1.2 互斥量
    ret = LOS_MuxCreate(&sensor_data_mux);
    if(ret != LOS_OK)
    {
        printf("[tasksInit] LOS_MuxCreate sensor_data_mux error, code: 0x%x\n", ret);
    }

    //1.3 队列

    //自动模式转换队列
    ret = LOS_QueueCreate("autoModeQ", 8, &autoMode_qId, 0, sizeof(event_mode_switch_t));
    if(ret != LOS_OK)
    {
        //error
        printf("[tasksInit] LOS_QueueCreate autoMode err, code: 0x%x\n", ret);
    }

    //控制中心队列
    ret = LOS_QueueCreate("cntlCenterQ", 32, &cntlCenter_qId, 0, sizeof(cntl_msg_t));
    if(ret != LOS_OK)
    {
        //error
        printf("[tasksInit] LOS_QueueCreate cntlCenter err, code: 0x%x\n", ret);
    }

    //门控制队列
    ret = LOS_QueueCreate("doorCntlQ", 8, &doorCntl_qId, 0, sizeof(unsigned char));
    if(ret != LOS_OK)
    {
        //error
        printf("[tasksInit] LOS_QueueCreate doorCntl err, code: 0x%x\n", ret);
    }

    //页面更新（按键）队列
    ret = LOS_QueueCreate("pageManageQ", 1, &pageManage_qId, 0, sizeof(KEY_TYPE));
    if(ret != LOS_OK)
    {
        //error
        printf("[tasksInit] LOS_QueueCreate pageManageQ err, code: 0x%x\n", ret);
    }

    //传感器数据传输用
    ret = LOS_QueueCreate("sensorDataQ", 2, &sensorData_qId, 0, sizeof(sensor_data_t));
    if(ret != LOS_OK)
    {
        //error
        printf("[tasksInit] LOS_QueueCreate sensorDataQ err, code: 0x%x\n", ret);
    }

    

    //2. 创建任务
    ret = LOS_TaskCreate(&task_autoMode_id, &task_autoMode_handler);
    if(LOS_OK != ret)
    {
        //error
        printf("[tasksInit] LOS_TaskCreate autoMode err, code: 0x%x\n", ret);
    }

    ret = LOS_TaskCreate(&task_cntlCenter_id, &task_cntlCenter_handler);
    if(LOS_OK != ret)
    {
        //error
        printf("[tasksInit] LOS_TaskCreate cntlCenter err, code: 0x%x\n", ret);
    }

    ret = LOS_TaskCreate(&task_bspInit_id, &task_bspInit_handler);
    if(LOS_OK != ret)
    {
        //error
        printf("[tasksInit] LOS_TaskCreate bspInit err, code: 0x%x\n", ret);
    }

    ret = LOS_TaskCreate(&task_doorCntl_id, &task_doorCntl_handler);
    if(LOS_OK != ret)
    {
        //error
        printf("[tasksInit] LOS_TaskCreate doorCntl err, code: 0x%x\n", ret);
    }

    ret = LOS_TaskCreate(&task_keyScan_id, &task_keyScan_handler);
    if(LOS_OK != ret)
    {
        //error
        printf("[tasksInit] LOS_TaskCreate keyScan err, code: 0x%x\n", ret);
    }

    ret = LOS_TaskCreate(&task_mqttHandle_id, &task_mqttHandle_handler);
    if(LOS_OK != ret)
    {
        //error
        printf("[tasksInit] LOS_TaskCreate mqttHandle err, code: 0x%x\n", ret);
    }

    ret = LOS_TaskCreate(&task_sensorData_id, &task_sensorData_handler);
    if(LOS_OK != ret)
    {
        //error
        printf("[tasksInit] LOS_TaskCreate sensorData err, code: 0x%x\n", ret);
    }

    ret = LOS_TaskCreate(&task_pageManage_id, &task_pageManage_handler);
    if(LOS_OK != ret)
    {
        //error
        printf("[tasksInit] LOS_TaskCreate pageManage err, code: 0x%x\n", ret);
    }

    LOS_TaskUnlock();
    printf("[tasksInit] Exit tasksInit.\n");
    //删除自己
    LOS_TaskDelete(tasksInit_id);
}



//看门狗 10s 喂一次



